import { ipcMain, IpcMainEvent, BrowserWindow, IpcMainInvokeEvent } from 'electron';
import { updateTool } from '../../main/tools/updateTool';
import { cacheManager } from '../../main/tools/cacheManager';

class Handler {
    initializeHandlers() {
        ipcMain.on('ping', this.handlePing.bind(this));
        ipcMain.handle('getTest', this.handleTest.bind(this));
        ipcMain.handle('autoUpdate', this.handleAutoUpdate.bind(this));
    }
    handleAutoUpdate = async (_event: IpcMainInvokeEvent,) => {
        updateTool.downloadUpdate();
        return;
    };
    handlePing = async (_event: IpcMainEvent, ...args: any[]) => {
        _event.reply('pong', 'Pong message from main process', args);
        // 获取当前窗口实例
        const mainWindow = BrowserWindow.getFocusedWindow();
        if (mainWindow) {
            mainWindow.webContents.send('progress', 100);
        }
        const windowSize: { width: number, height: number } = cacheManager.getCache("windowSize") as { width: number, height: number };
        if (!windowSize) {
            cacheManager.setCache("windowSize", { width: 1000, height: 1000 });
        } else {
            mainWindow?.setContentSize(windowSize.width, windowSize.height);
        }
    }
    handleTest = async (_event: IpcMainInvokeEvent, ...args: any[]): Promise<string> => {
        console.log('handleTest', !!_event, args);
        return 'test11111';
    }
}

// 导出单例
export const handler = new Handler();